﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
using System.Security.Cryptography;

/// <summary>
///UsersDao 的摘要说明
/// </summary>
public class UsersDao
{
    DBMM dbm = new DBMM();
    DataTable dt = null;
    string sql = null;
    UsersVo uv = new UsersVo();

    public static string MD5(string str)
    {
        byte[] b = Encoding.Default.GetBytes(str);
        b = new MD5CryptoServiceProvider().ComputeHash(b);
        string ret = "";
        for (int i = 0; i < b.Length; i++)
            ret += b[i].ToString("x").PadLeft(2, '0');
        return ret;
    }

    //添加新用户
    public Exception addUsers(UsersVo uv)
    {
        sql = "insert into Users(Uname,Upwd,Unickname,Uemail,Uphoto,Uregtime,Ulastvisit,Ugroup) values(@Uname,@Upwd,@Uname,@Uemail,'head.jpg',@Uregtime,@Uregtime,'普通用户')";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Uname", uv.Uname), 
                                                 new SqlParameter("@Upwd", MD5(uv.Upwd)), 
                                                 new SqlParameter("@Uemail", uv.Uemail), 
                                                 new SqlParameter("@Uregtime", DateTime.Now.ToString())};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //添加新店家用户
    public Exception addShopUsers(UsersVo uv)
    {
        sql = "insert into Users(Uname,Upwd,Unickname,Uemail,Utruename,Uidcard,Uphone,Uqq,Uphoto,Uregtime,Ulastvisit,Ugroup) values(@Uname,@Upwd,@Uname,@Uemail,@Utruename,@Uidcard,@Uphone,@Uqq,'head.jpg',@Uregtime,@Uregtime,'店家用户')";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Uname", uv.Uname), 
                                                 new SqlParameter("@Upwd", MD5(uv.Upwd)), 
                                                 new SqlParameter("@Uemail", uv.Uemail), 
                                                 new SqlParameter("@Utruename", uv.Utruename),
                                                 new SqlParameter("@Uidcard", uv.Uidcard),
                                                 new SqlParameter("@Uphone", uv.Uphone),
                                                 new SqlParameter("@Uqq", uv.Uqq),
                                                 new SqlParameter("@Uregtime", DateTime.Now.ToString())};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //修改用户信息
    public Exception updateUsers(UsersVo uv)
    {
        sql = "update Users set Unickname=@Unickname,Uemail=@Uemail,Usex=@Usex,Utruename=@Utruename,Uidcard=@Uidcard,Uphone=@Uphone,Utel=@Utel,Uaddress=@Uaddress,Uqq=@Uqq,Umsn=@Umsn,Usign=@Usign where Uid=@Uid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Unickname", uv.Unickname), 
                                                 new SqlParameter("@Uemail", uv.Uemail), 
                                                 new SqlParameter("@Usex", uv.Usex), 
                                                 new SqlParameter("@Utruename", uv.Utruename), 
                                                 new SqlParameter("@Uidcard", uv.Uidcard),
                                                 new SqlParameter("@Uphone", uv.Uphone),
                                                 new SqlParameter("@Utel", uv.Utel),
                                                 new SqlParameter("@Uaddress", uv.Uaddress),
                                                 new SqlParameter("@Uqq", uv.Uqq),
                                                 new SqlParameter("@Umsn", uv.Umsn),
                                                 new SqlParameter("@Usign", uv.Usign),
                                                 new SqlParameter("@Uid", uv.Uid)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //修改店家用户信息
    public Exception updateShopUsers(UsersVo uv)
    {
        sql = "update Users set Uemail=@Uemail,Utruename=@Utruename,Uidcard=@Uidcard,Uphone=@Uphone,Uqq=@Uqq where Uid=@Uid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Uemail", uv.Uemail), 
                                                 new SqlParameter("@Utruename", uv.Utruename), 
                                                 new SqlParameter("@Uidcard", uv.Uidcard),
                                                 new SqlParameter("@Uphone", uv.Uphone),
                                                 new SqlParameter("@Uqq", uv.Uqq),
                                                 new SqlParameter("@Uid", uv.Uid)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //增加用户积分
    public Exception updateUsersMoney(string id,string money)
    {
        sql = "update Users set Umoney=Umoney+@Umoney where Uid=@Uid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Umoney", money),
                                                 new SqlParameter("@Uid", id)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //修改用户头像
    public Exception updateUsersHead(string id, string url)
    {
        sql = "update Users set Uphoto=@Uphoto where Uid=@Uid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Uphoto", url),
                                                 new SqlParameter("@Uid", id)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //修改用户密码
    public Exception updateUsersPwd(string id, string pwd)
    {
        sql = "update Users set Upwd=@Upwd where Uid=@Uid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Upwd", MD5(pwd)),
                                                 new SqlParameter("@Uid", id)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //冻结单个用户
    public Exception updateUsersState(string id)
    {
        sql = "update Users set Ustate=1 where Uid=@Uid";
        SqlParameter[] sp = new SqlParameter[] {new SqlParameter("@Uid", id)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //用户登录
    public UsersVo UsersLogin(string name, string pwd)
    {
        //用户名密码判断
        sql = "select * from Users where Uname=@Uname and Upwd=@Upwd";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Uname", name), 
                                                 new SqlParameter("@Upwd",MD5(pwd))};
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        if (dt.Rows.Count > 0)
        {
            //判断登录时间
            DateTime t=Convert.ToDateTime(dt.Rows[0]["Ulastvisit"].ToString());
            if (t < DateTime.Now.AddDays(-1))
            {
                //增加积分
                updateUsersMoney(dt.Rows[0]["Uid"].ToString(),"5");
            }
            uv = new UsersVo();
            uv.Uid = Convert.ToInt32(dt.Rows[0][0]);
            uv.Uname = dt.Rows[0][1].ToString();
            uv.Upwd = dt.Rows[0][2].ToString();
            uv.Unickname = dt.Rows[0][3].ToString();
            uv.Uemail = dt.Rows[0][4].ToString();
            uv.Usex = dt.Rows[0][5].ToString();
            uv.Utruename = dt.Rows[0][6].ToString();
            uv.Uidcard = dt.Rows[0][7].ToString();
            uv.Uphone = dt.Rows[0][8].ToString();
            uv.Utel = dt.Rows[0][9].ToString();
            uv.Uaddress = dt.Rows[0][10].ToString();
            uv.Uqq = dt.Rows[0][11].ToString();
            uv.Umsn = dt.Rows[0][12].ToString();
            uv.Uphoto = dt.Rows[0][13].ToString();
            uv.Umoney = Convert.ToInt32(dt.Rows[0][14]);
            uv.Uregtime = dt.Rows[0][15].ToString();
            uv.Ulastvisit = dt.Rows[0][16].ToString();
            uv.Ugroup = dt.Rows[0][17].ToString();
            uv.Usign = dt.Rows[0][18].ToString();
            uv.Ustate = Convert.ToInt32(dt.Rows[0][19]);
            //修改最后登录时间
            sql = "update Users set Ulastvisit=@Ulastvisit where Uid=@Uid";
            sp = new SqlParameter[] { new SqlParameter("@Ulastvisit",DateTime.Now.ToString()),
                                      new SqlParameter("@Uid", dt.Rows[0][0].ToString())};
            dbm.Execute(sql, sp);
            return uv;
        }
        else
        {
            return null;
        }
    }

    //查找所有用户信息
    public DataTable getUsers()
    {
        sql = "select * from Users order by Uid desc";
        dt = new DataTable();
        dt = dbm.GetTable(sql);
        return dt;
    }

    //根据昵称模糊查找用户信息
    public DataTable getUsers(string name)
    {
        sql = "select * from Users where Unickname like @Unickname";
        SqlParameter[] sp = new SqlParameter[] {new SqlParameter("@Unickname", "%" + name + "%")};
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        return dt;
    }

    //检查用户名是否可用
    public string CheckName(string name)
    {
        sql = "select * from Users where Uname=@Uname";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Uname", name) };
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        if (dt.Rows.Count > 0)
        {
            string str = "对不起，用户名已存在！";
            return str;
        }
        else
        {
            string str = "恭喜，用户名可用！";
            return str;
        }
    }

    //通过id查询单个用户信息
    public UsersVo getUserById(string id)
    {
        sql = "select * from Users where Uid=@Uid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Uid", id) };
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        uv = new UsersVo();
        uv.Uid = Convert.ToInt32(dt.Rows[0][0]);
        uv.Uname = dt.Rows[0][1].ToString();
        uv.Upwd = dt.Rows[0][2].ToString();
        uv.Unickname = dt.Rows[0][3].ToString();
        uv.Uemail = dt.Rows[0][4].ToString();
        uv.Usex = dt.Rows[0][5].ToString();
        uv.Utruename = dt.Rows[0][6].ToString();
        uv.Uidcard = dt.Rows[0][7].ToString();
        uv.Uphone = dt.Rows[0][8].ToString();
        uv.Utel = dt.Rows[0][9].ToString();
        uv.Uaddress = dt.Rows[0][10].ToString();
        uv.Uqq = dt.Rows[0][11].ToString();
        uv.Umsn = dt.Rows[0][12].ToString();
        uv.Uphoto = dt.Rows[0][13].ToString();
        uv.Umoney = Convert.ToInt32(dt.Rows[0][14]);
        uv.Uregtime = dt.Rows[0][15].ToString();
        uv.Ulastvisit = dt.Rows[0][16].ToString();
        uv.Ugroup = dt.Rows[0][17].ToString();
        uv.Usign = dt.Rows[0][18].ToString();
        uv.Ustate = Convert.ToInt32(dt.Rows[0][19]);
        return uv;
    }
}
